package com.java.comprehensive.design.test.first;

import lombok.Data;

import java.util.Enumeration;
import java.util.Vector;

/**
 * @Author: Dgg
 * @Date: 2022/2/8 22:58
 * 示范了： Extarct Method(提取方法 ) replace conditional with polymorphism（用多态性替换条件）
 * self encapsulate field（自我封装领域 ）  Replace type code with state/strategy（用状态模式 替换代码）
 */
@Data
public class Customer {
	private String _name;
	private Vector _rentals = new Vector();

	public Customer(String _name) {
		this._name = _name;
	}

	public void addRental(Rental args) {
		_rentals.add(args);
	}

	public String statement() {
		double totalAmout = 0;
		int frequentRenterPoints = 0;
		Enumeration rentals = _rentals.elements();
		String result = "Rental Recora for," + get_name() + "\n";
		while (rentals.hasMoreElements()) {
			double thisAmount = 0;
			Rental each = (Rental) rentals.nextElement();
			// determine amounts for each line
			thisAmount = getThisAmount(thisAmount, each);

			frequentRenterPoints++;

			if ((each.get_movie().get_priceCode() == Movie.NEW_RELEASE) && each.get_daysRentd() > 1)
				frequentRenterPoints++;

			result += "\t" + each.get_movie().get_title() + "\t" + String.valueOf(thisAmount) + "\n";

			result += "You earned" + String.valueOf(frequentRenterPoints) + "frequent renter points";

		}
		return result;
	}

	/**
	 * idea 快捷键抽取
	 */
	private double getThisAmount(double thisAmount, Rental each) {
		switch (each.get_movie().get_priceCode()) {
			case Movie.REGULAR:
				thisAmount += 2;
				if (each.get_daysRentd() > 2) {
					thisAmount += (each.get_daysRentd() - 2) * 1.5;
				}
				break;
			case Movie.NEW_RELEASE:
				thisAmount += each.get_daysRentd() * 3;
				break;
			case Movie.CHILDRENS:
				thisAmount += 1.5;
				if (each.get_daysRentd() > 3) {
					thisAmount += (each.get_daysRentd() - 3) * 1.5;
				}
				break;
			default:
				System.out.println("未进入Switch");
				break;
		}
		return thisAmount;
	}

	/**
	 * 书上例子
	 */
	public String statementImprove() {
		int frequentRenterPoints = 0;
		Enumeration rentals = _rentals.elements();
		StringBuilder result = new StringBuilder("Rental Recora for," + get_name() + "\n");
		while (rentals.hasMoreElements()) {
			Rental each = (Rental) rentals.nextElement();
			// determine amounts for each line
			frequentRenterPoints += each.getFrequentRenterPoints();

			result.append("\t").append(each.get_movie().get_title()).append("\t").append(each.getCharge()).append("\n");
		}
		result.append("Amount owed is ").append(getTotalCharge()).append("\n");
		result.append("You earned  ").append(String.valueOf(getTotalFrequentRenterPoints())).append("  frequent renter points");
		return result.toString();
	}

	private double getTotalCharge() {
		double result = 0;
		Enumeration rentals = _rentals.elements();
		while (rentals.hasMoreElements()) {
			Rental each = (Rental) rentals.nextElement();
			result += each.getCharge();
		}
		return result;
	}

	private double getTotalFrequentRenterPoints() {
		int result = 0;
		Enumeration rentals = _rentals.elements();
		while (rentals.hasMoreElements()) {
			Rental each = (Rental) rentals.nextElement();
			result += each.getFrequentRenterPoints();
		}
		return result;
	}

	public static void main(String[] args) {
		Customer customer = new Customer("张三");
		Movie movie = new Movie();
		movie.set_title("复仇者联盟");
		movie.set_priceCode(Movie.NEW_RELEASE);
		Rental rental = new Rental();
		rental.set_daysRentd(5);
		rental.set_movie(movie);


		Movie movieTwo = new Movie();
		movieTwo.set_title("爱情神话");
		movieTwo.set_priceCode(Movie.REGULAR);
		Rental rentalTwo = new Rental();
		rentalTwo.set_movie(movieTwo);
		rentalTwo.set_daysRentd(10);
		customer.addRental(rental);
		customer.addRental(rentalTwo);

		System.out.println(customer.statementImprove());
	}

}
